package cn.edu.sdjzu.controller;



import cn.edu.sdjzu.service.CalculatorService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RefreshScope

public class CalculatorController {
    @Autowired
    private CalculatorService calculatorService;

    @GetMapping("/add")
    @SentinelResource(value = "add", blockHandler = "addBlockHandler", fallback = "addFallback")
    public String add(@RequestParam Integer a, @RequestParam Integer b) {
        return "计算结果为"+calculatorService.add(a, b);
    }

    public String addBlockHandler(Integer a, Integer b, BlockException ex) {
        log.error("触发熔断降级: {}", ex.getMessage());
        return "服务繁忙，请稍后重试";
    }

    public String addFallback(Integer a, Integer b, Throwable ex) {
        log.error("服务异常降级: {}", ex.getMessage());
        return "服务异常，请稍后重试";
    }
}
